// const app = getApp()

var app = getApp()

Page({
    data: {
        array: [{
            mode: '',
            text: '',
        }],
        src: '../../img/banner.png',
        freeList: [],
        page:1,         // 页码

        loading:false,      // 加载中
        noData:false,       // 没有数据
        noMore:false,       // 没有更多
        hint:false,         // 是否提示

		user_info:"",		// 用户数据

        enterStatus:"",     // 入驻状态
    },
	getBanner(res){
		var self = this;
		wx.showLoading({ title: '加载中' })
		wx.request({
			url: app.url.requestUrl + '/index/getBanner',
			method: 'POST',
			data: {
				openid: res.data.openid,
				token: res.data.token,
                type:2
			},
			success: function (resbanner) {
				if (resbanner.data.code == 1) {
					self.setData({
						src: resbanner.data.data.img
					})
				}else{
					if (resbanner.data.code == -1 || resbanner.data.code == 404) {
						self.loginOverdue();
					}
				}
			}
		})
	},
    getfreeList(userInfo){
        var self = this;
        var noData = false;                        // 是否没有数据
        var noMore = false;                        // 是否没有更多
        var page = this.data.page;
        var datas = this.data.freeList;            // 原数据

		wx.request({
			url: app.url.requestUrl + '/index/getbulk',
			method: 'POST',
			data: {
				openid: userInfo.data.openid,
				token: userInfo.data.token,
				page: page
			},
			success: function (res) {
				var list = res.data;
				if (list.data || datas.length) {
					noData = 0;
				} else {
					noData = 1;
				}
				self.setData({
					noData: noData
				})
				if (list.code == 1) {
                    if (!list.data.hasNext) {
						self.setData({ noMore: true })
					}
                    list.data.data.map(function (item, index) {
                        item.score = app.globalData.levelRuleCount(item.score);
                    })
					self.setData({
						freeList: datas.concat(list.data.data),
						loading: false
					})
				}
                setTimeout(function(){
                    wx.stopPullDownRefresh()
                    wx.hideLoading()
                },300)
			}
		})
    },
    // 获取入驻状态
    getEnterStatus(res) {
        var self = this;
        var url = "/user/check_status";

        wx.request({
            url: app.url.requestUrl + url,
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
            },
            success: function (status) {
                if (status.data.code == 1) {
                    self.setData({
                        enterStatus: status.data.data
                    })
                }
            }
        })
    },
    onLoad: function() {
		var that = this;
		wx.getStorage({
			key: 'user',
			success: function (res) {
				that.getBanner(res);
				that.getfreeList(res);
                that.getEnterStatus(res);
				that.setData({user_info: res})
			},
			fail() {
				wx.login({
					success: res => {
						console.log("fail")
						wx.request({
							url: app.url.requestUrl + '/login/login',
							method: 'POST',
							data: {
								code: res.code
							},
							success: function (data) {
								if (data.data.code == 1) {
									wx.setStorage({
										key: 'user',
										data: data.data.data
									})
                                    that.getEnterStatus(data.data);
									that.getBanner(data.data);
									that.getfreeList(data.data);
                                    that.setData({ user_info: data.data })
								}
							}
						})
					}
				})
			}
		})
    },
	loginOverdue(){
		var self = this;
		wx.removeStorage({
			key: 'user',
			success: function (res) {
				self.onLoad();
			},
		})
	},
    // 下拉刷新
    onPullDownRefresh(){
        this.setData({
            freeList:[],
            page:1
        })
        this.onLoad();
    },
    // 上拉加载更多
    onReachBottom(){
        var page = ++this.data.page;
        var noMore = this.data.noMore;
        var loading = this.data.loading;
		var user_info = this.data.user_info;
        if (!noMore && !loading) {
            this.setData({ page: page, loading: true })
            wx.showLoading({ title: '加载中' })
			this.getfreeList(user_info);
        } else {
            if (!this.data.hint) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '暂无更多',
                })
                this.setData({ hint: true })
            }
        }
    }
})